<?php
add_filter('wb_sanitize_text', 'sanitize_text_field');
function wb_sanitize_textarea($input) {
    global $allowedposttags;
    $custom_allowedtags["embed"] = array(
        "src"               => array(),
        'script'            => array(),
        'style'             => array(),
        "type"              => array(),
        "allowfullscreen"   => array(),
        "allowscriptaccess" => array(),
        "height"            => array(),
        "width"             => array(),
    );
    $custom_allowedtags["script"] = array("type" => array(), "src" => array());
    $custom_allowedtags           = array_merge($custom_allowedtags, $allowedposttags);
    $output                       = wp_kses($input, $custom_allowedtags);
    return $output;
}
add_filter('wb_sanitize_textarea', 'wb_sanitize_textarea');
add_filter('wb_sanitize_select', 'wb_sanitize_enum', 10, 2);
add_filter('wb_sanitize_radio', 'wb_sanitize_enum', 10, 2);
add_filter('wb_sanitize_images', 'wb_sanitize_enum', 10, 2);
function wb_sanitize_checkbox($input) {
    if ($input) {
        $output = '1';
    } else {
        $output = false;
    }
    return $output;
}
add_filter('wb_sanitize_checkbox', 'wb_sanitize_checkbox');
function wb_sanitize_multicheck($input, $option) {
    $output = '';
    if (is_array($input)) {
        foreach ($option['options'] as $key => $value) {
            $output[$key] = "0";
        }
        foreach ($input as $key => $value) {
            if (array_key_exists($key, $option['options']) && $value) {
                $output[$key] = "1";
            }
        }
    }
    return $output;
}
add_filter('wb_sanitize_multicheck', 'wb_sanitize_multicheck', 10, 2);
add_filter('wb_sanitize_color', 'wb_sanitize_hex');
function wb_sanitize_upload($input) {
    $output   = '';
    $filetype = wp_check_filetype($input);
    if ($filetype["ext"]) {
        $output = $input;
    }
    return $output;
}
add_filter('wb_sanitize_upload', 'wb_sanitize_upload');
function wb_sanitize_editor($input) {
    if (current_user_can('unfiltered_html')) {
        $output = $input;
    } else {
        global $allowedtags;
        $output = wpautop(wp_kses($input, $allowedtags));
    }
    return $output;
}
add_filter('wb_sanitize_editor', 'wb_sanitize_editor');
function wb_sanitize_allowedtags($input) {
    global $allowedtags;
    $output = wpautop(wp_kses($input, $allowedtags));
    return $output;
}
function wb_sanitize_allowedposttags($input) {
    global $allowedposttags;
    $output = wpautop(wp_kses($input, $allowedposttags));
    return $output;
}

add_filter('wb_sanitize_info', 'wb_sanitize_allowedposttags');
function wb_sanitize_enum($input, $option) {
    $output = '';
    if (array_key_exists($input, $option['options'])) {
        $output = $input;
    }
    return $output;
}
function wb_sanitize_background($input) {
    $output = wp_parse_args($input, array(
        'color'      => '',
        'image'      => '',
        'repeat'     => 'repeat',
        'position'   => 'top center',
        'attachment' => 'scroll',
    ));

    $output['color']      = apply_filters('wb_sanitize_hex', $input['color']);
    $output['image']      = apply_filters('wb_sanitize_upload', $input['image']);
    $output['repeat']     = apply_filters('wb_background_repeat', $input['repeat']);
    $output['position']   = apply_filters('wb_background_position', $input['position']);
    $output['attachment'] = apply_filters('wb_background_attachment', $input['attachment']);

    return $output;
}
add_filter('wb_sanitize_background', 'wb_sanitize_background');
function wb_sanitize_background_repeat($value) {
    $recognized = wb_recognized_background_repeat();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_background_repeat', current($recognized));
}
add_filter('wb_background_repeat', 'wb_sanitize_background_repeat');

function wb_sanitize_background_position($value) {
    $recognized = wb_recognized_background_position();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_background_position', current($recognized));
}
add_filter('wb_background_position', 'wb_sanitize_background_position');

function wb_sanitize_background_attachment($value) {
    $recognized = wb_recognized_background_attachment();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_background_attachment', current($recognized));
}
add_filter('wb_background_attachment', 'wb_sanitize_background_attachment');
function wb_sanitize_typography($input, $option) {

    $output = wp_parse_args($input, array(
        'size'  => '',
        'face'  => '',
        'style' => '',
        'color' => '',
    ));

    if (isset($option['options']['faces']) && isset($input['face'])) {
        if (!(array_key_exists($input['face'], $option['options']['faces']))) {
            $output['face'] = '';
        }
    } else {
        $output['face'] = apply_filters('wb_font_face', $output['face']);
    }

    $output['size']  = apply_filters('wb_font_size', $output['size']);
    $output['style'] = apply_filters('wb_font_style', $output['style']);
    $output['color'] = apply_filters('wb_sanitize_color', $output['color']);
    return $output;
}
add_filter('wb_sanitize_typography', 'wb_sanitize_typography', 10, 2);
function wb_sanitize_font_size($value) {
    $recognized  = wb_recognized_font_sizes();
    $value_check = preg_replace('/px/', '', $value);
    if (in_array((int) $value_check, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_font_size', $recognized);
}
add_filter('wb_font_size', 'wb_sanitize_font_size');
function wb_sanitize_font_style($value) {
    $recognized = wb_recognized_font_styles();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_font_style', current($recognized));
}
add_filter('wb_font_style', 'wb_sanitize_font_style');
function wb_sanitize_font_face($value) {
    $recognized = wb_recognized_font_faces();
    if (array_key_exists($value, $recognized)) {
        return $value;
    }
    return apply_filters('wb_default_font_face', current($recognized));
}
add_filter('wb_font_face', 'wb_sanitize_font_face');
function wb_recognized_background_repeat() {
    $default = array(
        'no-repeat' => '不重复填充',
        'repeat-x'  => '水平重复填充',
        'repeat-y'  => '垂直重复填充',
        'repeat'    => '重复填充',
    );
    return apply_filters('wb_recognized_background_repeat', $default);
}
function wb_recognized_background_position() {
    $default = array(
        'top left'      => '上左对齐',
        'top center'    => '上中对齐',
        'top right'     => '上右对齐',
        'center left'   => '中左对齐',
        'center center' => '中中对齐',
        'center right'  => '中右对齐',
        'bottom left'   => '下左对齐',
        'bottom center' => '下中对齐',
        'bottom right'  => '下右对齐',
    );
    return apply_filters('wb_recognized_background_position', $default);
}
function wb_recognized_background_attachment() {
    $default = array(
        'scroll' => '固定',
        'fixed'  => '滚动',
    );
    return apply_filters('wb_recognized_background_attachment', $default);
}
function wb_sanitize_hex($hex, $default = '') {
    if (wb_validate_hex($hex)) {
        return $hex;
    }
    return $default;
}
function wb_recognized_font_sizes() {
    $sizes = range(12, 24, 2);
    $sizes = apply_filters('wb_recognized_font_sizes', $sizes);
    $sizes = array_map('absint', $sizes);
    return $sizes;
}
function wb_recognized_font_faces() {
    $default = array(
        'arial'     => 'Arial',
        'verdana'   => 'Verdana, Geneva',
        'trebuchet' => 'Trebuchet',
        'georgia'   => 'Georgia',
        'times'     => 'Times New Roman',
        'tahoma'    => 'Tahoma, Geneva',
        'palatino'  => 'Palatino',
        'helvetica' => 'Helvetica*',
    );
    return apply_filters('wb_recognized_font_faces', $default);
}
function wb_recognized_font_styles() {
    $default = array(
        'normal'      => '普通',
        'italic'      => '斜体',
        'bold'        => '粗体',
        'bold italic' => '斜体+粗体',
    );
    return apply_filters('wb_recognized_font_styles', $default);
}
function wb_validate_hex($hex) {
    $hex = trim($hex);
    if (0 === strpos($hex, '#')) {
        $hex = substr($hex, 1);
    } elseif (0 === strpos($hex, '%23')) {
        $hex = substr($hex, 3);
    }
    if (0 === preg_match('/^[0-9a-fA-F]{6}$/', $hex)) {
        return false;
    } else {
        return true;
    }
}